/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.StringUtil;
import com.ntb.models.UsersDAO;
import com.ntb.models.impl.UsersDAOImpl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class GetNewPasswordAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
        ActionForward forward = mapping.getInputForward();

        int userid =  Integer.parseInt(request.getParameter("user"));
        String forgottenPasswordCode = request.getParameter("secure");

        UsersDAO usersDAO = new UsersDAOImpl();
        
        if (usersDAO.checkForgotPasswordCode(new StringUtil().generateMD5(forgottenPasswordCode), userid) > 0) {
            usersDAO.updatePassword(userid);
            usersDAO.updateForgotPassword("", "", userid);
            request.setAttribute("successMessage", userid);
        }

        return forward;
    }
}
